package com.ds.lens.data.service.dao;

import com.ds.lens.data.service.dao.entity.LensMonitorChannel;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

@Repository
public interface LensMonitorChannelDao {

    String COLUMNS = " id, monitor_id, version, target, type, ticket, ticket_time ";

    @Insert({
            "insert into lens_monitor_channel (monitor_id, version, target, type, ticket, ticket_time)",
            "values (#{monitorId,jdbcType=BIGINT}, #{version,jdbcType=INTEGER}, ",
            "#{target,jdbcType=VARCHAR}, #{type,jdbcType=SMALLINT}, #{ticket}, #{ticketTime,jdbcType=TIMESTAMP})",
            " ON DUPLICATE KEY UPDATE ticket=#{ticket}, ticket_time=#{ticketTime} "
    })
    int insertOrUpdate(LensMonitorChannel record);

    @Select({
            "select",
            COLUMNS,
            "from lens_monitor_channel",
            "where id = #{id,jdbcType=BIGINT}"
    })
    @Results(id = "monitorChannel", value = {
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "monitor_id", property = "monitorId", jdbcType = JdbcType.BIGINT),
            @Result(column = "version", property = "version", jdbcType = JdbcType.INTEGER),
            @Result(column = "type", property = "type", jdbcType = JdbcType.SMALLINT),
            @Result(column = "target", property = "target", jdbcType = JdbcType.VARCHAR),
            @Result(column = "ticket", property = "ticket", jdbcType = JdbcType.VARCHAR),
            @Result(column = "ticket_time", property = "ticketTime", jdbcType = JdbcType.TIMESTAMP)
    })
    LensMonitorChannel selectByPrimaryKey(Long id);

    @Select({
            "select ",
            COLUMNS,
            "from lens_monitor_channel",
            "where monitor_id = #{monitorId} and version = #{version} and target = #{target} and type = #{type}"
    })
    @ResultMap("monitorChannel")
    LensMonitorChannel selectByMonitor(@Param("monitorId") Long monitorId, @Param("version") Integer version,
                                                     @Param("target") String target, @Param("type") Integer type);
}
